package com.example.mycarcalling.controller;


import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mycarcalling.bean.Netease;
import com.example.mycarcalling.service.NeteaseService;
import com.example.mycarcalling.utils.BaseResponse;
import com.example.mycarcalling.utils.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sun.dc.pr.PRError;

import javax.annotation.Resource;

@RestController
@BaseResponse
@RequestMapping(value = "netease")
public class NeteaseController {

    @Resource
    private NeteaseService neteaseService;

    @RequestMapping(method = RequestMethod.GET,value = "getNeteaseToken")
    public ResponseResult getNeteaseToken(){
        Netease netease = null;
        try {
            netease = neteaseService.getUserNetease(StpUtil.getLoginIdAsLong());
            if(null==netease){
                throw new Exception("没有同步的IM令牌");
            }
            return new ResponseResult(200,"success",netease.getImToken());
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            return new ResponseResult(500, e.getMessage(), null);
        }
    }
}
